{% extends 'base.html' %}

{% block title %}素材库 - 广告主{% endblock %}

{% block content %}
<style>
    /* 样式定义 */
    .library-container {
        max-width: 1000px;
        margin: 0 auto;
        background: #fff;
        padding: 30px;
        border-radius: 12px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.08);
        font-family: 'Segoe UI', sans-serif;
    }

    .library-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
    }

    .library-header h2 {
        margin: 0;
        font-size: 24px;
        color: #2c3e50;
    }

    .upload-form {
        display: flex;
        gap: 10px;
        align-items: center;
        margin-bottom: 30px;
        flex-wrap: wrap;
    }

    .upload-form input,
    .upload-form select {
        padding: 8px 12px;
        border: 1px solid #ddd;
        border-radius: 6px;
        font-size: 14px;
    }

    .upload-form button {
        background-color: #6c5ce7;
        color: white;
        border: none;
        padding: 10px 18px;
        border-radius: 6px;
        cursor: pointer;
        transition: background 0.3s;
    }

    .upload-form button:hover {
        background-color: #5a4bd3;
    }

    .material-list {
        display: flex;
        flex-wrap: wrap;
        gap: 20px;
    }

    .material-item {
        width: 220px;
        background: #f8f9fa;
        padding: 15px;
        border-radius: 8px;
        box-shadow: 0 2px 5px rgba(0,0,0,0.05);
    }

    .material-preview {
        width: 100%;
        height: 120px;
        object-fit: cover;
        border-radius: 4px;
        margin-bottom: 10px;
    }

    .material-details {
        font-size: 13px;
        color: #333;
        margin-bottom: 10px;
    }

    .material-actions button {
        width: 100%;
        padding: 8px;
        background: #e74c3c;
        color: white;
        border: none;
        border-radius: 4px;
        cursor: pointer;
    }

    .empty-state {
        text-align: center;
        width: 100%;
        color: #999;
        padding: 50px 0;
    }
</style>

<div class="library-container">
    <div class="library-header">
        <h2>🎞️ 我的素材库</h2>
    </div>

    <form method="post" action="{% url 'sourcematerial:upload_material' %}" enctype="multipart/form-data" class="upload-form">
        {% csrf_token %}
        <input type="file" name="file" required>
        <select name="material_type">
            <option value="image">图片</option>
            <option value="video">视频</option>
        </select>
        <button type="submit">📤 上传素材</button>
    </form>

    <div class="material-list">
        {% for material in materials %}
        <div class="material-item">
            {% if material.material_type == 'image' %}
                <img src="{{ material.file.url }}" alt="Material Preview" class="material-preview">
            {% elif material.material_type == 'video' %}
                <video class="material-preview" controls>
                    <source src="{{ material.file.url }}" type="video/mp4">
                    浏览器不支持播放视频
                </video>
            {% endif %}

            <div class="material-details">
                <div><strong>类型：</strong>{{ material.get_material_type_display }}</div>
                <div><strong>路径：</strong>{{ material.file.url }}</div>
            </div>

            <div class="material-actions">
                <button onclick="deleteMaterial({{ material.id }})">删除</button>
            </div>
        </div>
        {% empty %}
        <div class="empty-state">您还没有上传任何素材</div>
        {% endfor %}
    </div>
</div>

<script>
    function deleteMaterial(materialId) {
        if (confirm('确定要删除这个素材吗？')) {
            fetch(`/materials/delete/${materialId}/`, {
                method: 'POST',
                headers: {
                    'X-CSRFToken': '{{ csrf_token }}'
                }
            }).then(response => {
                if (response.ok) {
                    alert('素材已删除');
                    window.location.reload();
                } else {
                    alert('删除失败');
                }
            });
        }
    }
</script>
{% endblock %}